package graphics.fingerprint;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import toolkit.SystemInfo;
import command.SimpleMessage;
import command.SimpleActionCommand;
import connection.Connection;
import connection.InternetConnection;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.LinearLayout;

public class FingerPaint extends Activity {
	PaintView pv = null;

	private Socket socket;
	private CommandPusher commandPusher;
	private ImagePuller imagePuller;
	private UserController controller;

	private Button btnMode;
	private Button btnPointer;

	public static DisplayMetrics dm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		btnInit();

		pv = new PaintView(this);
		this.commandPusher = new CommandPusher();
		this.imagePuller = new ImagePuller(pv.getSourceHandler());
		this.controller = new UserController(pv, this.commandPusher);
		pv.setController(this.controller);

		LinearLayout canvas = (LinearLayout) this.findViewById(R.id.canvas);
		canvas.addView(pv);

		dm = SystemInfo.getDisplayMetrics(pv.getContext());
		this.commandPusher.pushCommand(new SimpleMessage(dm.toString()));

		this.startWork();
	}

	private void btnInit() {
		Button btnExit = (Button) this.findViewById(R.id.exit);
		Button btnRfrs = (Button) this.findViewById(R.id.refresh);
		Button btnRott = (Button) this.findViewById(R.id.rotate);
		btnMode = (Button) this.findViewById(R.id.mode);
		btnPointer = (Button) this.findViewById(R.id.pointer);

		btnRfrs.setOnClickListener(new RefreshListener());
		btnExit.setOnClickListener(new ExitListener());
		btnRott.setOnClickListener(new RotateListener());
		btnMode.setOnClickListener(new ModeChangeListener());
		btnPointer.setOnClickListener(new PointerChangeListener());
	}

	private void startWork() {
		new Thread(new TryConnect()).start();
	}

	private class TryConnect implements Runnable {
		@Override
		public void run() {
			try {
				Connection connection = new InternetConnection(
						"175.186.33.102", 54321);
				socket = connection.getSocket();
				commandPusher.startSend(socket);
				imagePuller.startReceive(socket);
			} catch (UnknownHostException e) {
				System.exit(0);
				e.printStackTrace();
			} catch (IOException e) {
				System.exit(0);
				e.printStackTrace();
			}
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return true;
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_UP:
			commandPusher.pushCommand(SimpleActionCommand.BACK);
			break;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			commandPusher.pushCommand(SimpleActionCommand.NEXT);
			break;
		}
		return false;
	}

	private class ModeChangeListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			controller.changeMode();
			String txt = "";
			if (controller.getMode() == UserController.MODE_MARK) {
				txt = "Mark";
				btnPointer.setClickable(true);
			} else if (controller.getMode() == UserController.MODE_MOVE) {
				txt = "Move";
				btnPointer.setClickable(false);
			}
			btnMode.setText(txt);
		}
	}

	private class PointerChangeListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			controller.changePointer();
			String txt = "";
			if (controller.getPointer() == UserController.POINTER_ARROW) {
				txt = "Arrow";
			} else if (controller.getPointer() == UserController.POINTER_PEN) {
				txt = "Pen";
			} else if (controller.getPointer() == UserController.POINTER_ERASER) {
				txt = "Eraser";
			}
			btnPointer.setText(txt);
		}
	}

	private class ExitListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			try {
				commandPusher.pushCommand(SimpleActionCommand.CLOSE);
				Thread.sleep(300);
				imagePuller.stopWork();
				commandPusher.stopWork();
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				System.exit(0);
			}
		}
	}

	private class RefreshListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			commandPusher.pushCommand(SimpleActionCommand.REFRESH);
		}
	}

	private class RotateListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			commandPusher.pushCommand(SimpleActionCommand.ROTATE);
		}
	}

	public View getView() {
		return this.pv;
	}
}